// EndPointDisplace.cpp: implementation of the CEndPointDisplace class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EndPointDisplace.h"
#include "RecordsetEx.h"
#include "PPUser.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define MAX_DISP 9999.99
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CEndPointDisplace::CEndPointDisplace()
{
	pRs=NULL;
}

CEndPointDisplace::~CEndPointDisplace()
{

}

void CEndPointDisplace::GetEndPointDisplace(_RecordsetPtr &pRs)
{
	CString strSQL=_T("");
	long VECTOR_NUM=1;
	CString str=_T("");
	CRecordsetEx REx;
	REx.m_pCon=m_pCon;
	for(int n=1;n<10;n++)
	{
		str.Format(_T("D%d"),n);
		if(m_strRGKname.Find(str)!=-1)
		{
			VECTOR_NUM=n;
			break;
		}
	}
	/*
	strSQL.Format("SELECT From_node, To_node, y.node_num AS node, \
		dx, dy, dz, Rx, Ry, Rz FROM INPUT_BASIC_ELEMENT_DATA AS x, \
		INPUT_DISPLMNT AS y WHERE x.DISP_PTR=y.DISP_PTR and y.VECTOR_NUM=%d",VECTOR_NUM);
	*/
	strSQL.Format(_T("select * from input_displmnt where vector_num=%d"),VECTOR_NUM);
	try
	{
		REx.OpenTable(pRs,strSQL);
	}
	catch(_com_error e)
	{
		#ifdef _DEBUG
			AfxMessageBox(e.Description());
		#endif
	}
}

void CEndPointDisplace::OpenTableWY(_RecordsetPtr& pRs)
{
	CRecordsetEx REx;
	CString strSQL=_T("");
	try
	{
		strSQL=_T("select * from wy");
		REx.m_pCon=m_pPDCon;
		REx.OpenTable(pRs,strSQL);
	}
	catch(_com_error e)
	{
		#ifdef _DEBUG
		AfxMessageBox(e.Description());
		#endif
	}
}

bool CEndPointDisplace::WriteEndPointDisplace()
{
	CString strSQL;
	

	_RecordsetPtr pRsPoint=NULL;
	_RecordsetPtr pRsDisp=NULL;
	long PSAnode=0;
	try
	{
		strSQL=_T("delete * from wy");
		m_pPDCon->Execute(_bstr_t(strSQL),NULL,adCmdText);

		OpenTableWY(pRs);
		m_ConvertD.InitUnits(_T("LineDispUnitName"),_T("LineDispUnitScale"),_T(""));
		m_ConvertD.m_pCon=m_pUnitsCon;

		m_ConvertA.InitUnits(_T("AngleDispUnitName"),_T("AngleDispUnitScale"),_T(""));
		m_ConvertA.m_pCon=m_pUnitsCon;

		GetEndPointDisplace(pRsDisp);
		GetEndPoint(pRsPoint);
		if(!m_ConvertD.UnitsConvert(m_DispUnitsPSA,m_DispUnitsPD)) return false;
		if(!m_ConvertA.UnitsConvert(m_AngleUnitsPSA,m_AngleUnitsPD)) return false;
		for(;!pRsPoint->adoEOF;pRsPoint->MoveNext())
		{
			PSAnode=(long)vtof(pRsPoint->GetCollect(_T("PSAnode")));
			strSQL.Format(_T("NODE_NUM=%d"),PSAnode);
			pRsDisp->Filter=_T("");
			int i=pRsDisp->GetRecordCount();
			pRsDisp->Filter=_bstr_t(strSQL);
			i=pRsDisp->GetRecordCount();
			/*
			if(pRsDisp->adoEOF&&pRsDisp->BOF)
			{
				strSQL.Format(_T("To_node=%s"),strNode);
				pRsDisp->Filter=_T("");
				pRsDisp->Filter=_bstr_t(strSQL);
			}
			*/
			writeWY(pRsDisp);
		}
	}
	catch(_com_error e)
	{
		#ifdef _DEBUG
		AfxMessageBox(e.Description());
		#endif
	}
	return true;
}

void CEndPointDisplace::writeWY(_RecordsetPtr &pRsDisp)
{
	try
	{
		if(pRsDisp->GetRecordCount()>0)
		{
			pRs->AddNew();
			pRs->PutCollect(_T("pt"),pRsDisp->GetCollect(_T("NODE_NUM")));
			WriteFields(pRsDisp,_T("DX"),_T("dx"));
			WriteFields(pRsDisp,_T("DY"),_T("dY"));
			WriteFields(pRsDisp,_T("DZ"),_T("dZ"));
			WriteFields(pRsDisp,_T("RX"),_T("Rx"));
			WriteFields(pRsDisp,_T("RY"),_T("RY"));
			WriteFields(pRsDisp,_T("RZ"),_T("RZ"));
			pRs->Update();
		}
	}
	catch(_com_error e)
	{
		#ifdef  _DEBUG
		AfxMessageBox(e.Description());
		#endif
	}

}

void CEndPointDisplace::WriteFields(_RecordsetPtr &pRsDisp, CString strS, CString strT)
{
	float var=0.0;
	try
	{
		var=vtof(pRsDisp->GetCollect(_variant_t(strS)));
		if ( abs(var - MAX_DISP) < 0.01 )
		{

		}
		else
		{
			if(strS.Left(1)==_T("D"))
			{
				var=m_ConvertD.ConvertValue(var);
			}
			else
			{
				var=m_ConvertA.ConvertValue(var);
			}
			pRs->PutCollect(_variant_t(strT),_variant_t(var));	
		}

	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
}
